<?php
/**
 * 
 * Helper responsaval para gerar o DataGrid com padrao de layout do sistema.
 * 
 * @author		Altamiro Rodrigues, felipe moura
 * @package		views
 * @subpackage	helpers
 * @since		13.07.2010
 */ 
class Zend_View_Helper_DataGrid  extends Zend_View_Helper_Abstract
{
	protected $_arrColumn		=	array();
	protected $_arrAttrColumn	=	array();
	
	protected $_arrRow 			=	array();
	protected $_arrAttrRow		=	array();
	
	protected $_arrAttrRowTr	=	array();
	
	protected $_arrMergeTable	=	array();
	protected $_arrAttrTable	=	array( 'style'			=> 'width:100%' , 
										   'cellspacing'	=> '3' , 
										   'cellpadding'	=> '3' , 
										   'border'			=> '0' );
	protected $thead			= array();
	protected $tbody			= array();
	protected $tfoot			= array();
	protected $row				= array();
	
	 
	
	public function dataGrid( $strTabelaAtributo = array() )
	{
		$this->_arrMergeTable = array_merge( $this->_arrAttrTable , $strTabelaAtributo );
		
		return $this;
	} # end method dataGrid
	
	public function addColumnThead( $strName , $arrAttr = array() )
	{
		$th	= new Voitech_Element('th');
		$th->addAttrs( $arrAttr );
		$th->setValue( $strName );
		$this->thead[] = $th->show();
	} # end method addColumn
	
	public function addColumnTbody( $strCampo , $arrAttr = array() )
	{		
		$td	= new Voitech_Element('td');		
		$td->addAttrs( $arrAttr );
		$td->setValue( $strCampo );
		$this->row[] = $td->show();
	} # end method addRow
	
	public function setTr( $arrAttr = array() )
	{
		$tr		= new Voitech_Element('tr');
		$tr->addAttrs( $arrAttr );
		foreach( $this->row as $valor )
		{
			$tr->setValue( $valor );
		}
		$this->row = null;
		$this->tbody[] = $tr; 
	}
	
	/**
	 * Metodo que monta o cabeçalho da grid
	 */
	protected function compositeThead()
	{
		$thead  = new Voitech_Element('thead');
		$tr		= new Voitech_Element('tr');
		$tr->addAttrs( array( 'class' => 'corTitulo' , 'bgcolor' => '#4AA6D7' ) );
		
		if ( count( $this->thead ) > 0 )
		{
			foreach( $this->thead as $valor )
			{
				$tr->setValue( $valor );
			}
			$thead->setValue( $tr->show() );
		} # end iF.
		
		return $thead->show();
	}
	
/**
	 * Metodo que monta o corpo da grid
	 */
	protected function compositeTbody()
	{
		
		$tbody  = new Voitech_Element('tbody');

		if( count( $this->tbody ) > 0 )
		{
			foreach( $this->tbody as $chave=>$tr )
			{
				$strClass = ( $chave % 2 == 0 ) ? "corDetalhe_1" : "corDetalhe_2";
				$tr->addAttr( 'class' , $strClass);
				$tbody->setValue( $tr->show() );
			}
		} # end iF;		
		return $tbody->show();
	}
	
	/**
	 * Metodo que monta o rodape da grid
	 */
	protected function compositeTfoot()
	{
		# verificando se esta setado o view partial e setando  
		if( is_null( Zend_View_Helper_PaginationControl::getDefaultViewPartial() ) )
		{
			# setando o view partial default
			Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
		}
		
		if ( count( $this->view->paginator ) )
		{
			if ( $this->view->paginator->getTotalItemCount() >= $this->view->paginator->getItemCountPerPage() ) 
			{
				$tfoot  = new Voitech_Element('tfoot');
				$tr		= new Voitech_Element('tr');
				$td     = new Voitech_Element('td');
				$td->addAttr( 'colspan', count( $this->thead ) );
				$td->setValue( $this->view->paginationControl() );
				$tr->setValue( $td->show() );
				$tfoot->setValue( $tr->show() );
				return $tfoot->show();
			} # end iF;
		}
		else if ( count( $this->tbody ) == 0 )
		{
			$tfoot  = new Voitech_Element('tfoot');
			$tr		= new Voitech_Element('tr');
			$td     = new Voitech_Element('td');
			$tr->addAttr( 'class','corDetalhe2' );
			$td->addAttr( 'colspan', count( $this->thead ) );
			$td->addAttr( 'style','text-align: center; font-weight: bold;font-size:12px;' );
			$td->addAttr( 'class','corDetalhe2' );			
			$td->setValue( 'Nenhum registro encontrado!' );
			$tr->setValue( $td->show() );
			$tfoot->setValue( $tr->show() );
			return $tfoot->show();
		} # end iF.
		
	} # end method tfoot
	
	public function show()
	{
		$table = new Voitech_Element('table');
		$table->addAttrs( $this->_arrMergeTable );
		$table->setValue( $this->compositeThead() );
		$table->setValue( $this->compositeTbody() );
		$table->setValue( $this->compositeTfoot() );
		return $table->show();
	} # end method show
	
}